package com.qys.training.server.controller;

import com.qys.training.base.dto.BaseResult;
import com.qys.training.base.enumerate.BizCodeEnum;
import com.qys.training.base.util.BaseUtils;
import com.qys.training.biz.auth.criteria.UserCriteria;
import com.qys.training.biz.auth.entity.User;
import com.qys.training.biz.auth.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    public static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 新建
     * @param user
     * @return
     */
    @PostMapping(path = "/create")
    public @ResponseBody
    BaseResult create(@RequestBody User user){
        Long id = this.userService.create(user);
        return BaseResult.success(id);
    }

    /**
     * 身份认证
     * @param loginUser
     * @return
     */
    @PostMapping("/login")
    public @ResponseBody
    BaseResult login(@RequestBody User loginUser) {
        UserCriteria criteria = new UserCriteria();
        criteria.setUsername(loginUser.getUsername());
        criteria.setPassword(BaseUtils.stringToMD5(loginUser.getPassword()));

        User user = userService.findByCriteria(criteria);
        if(user == null) {
            return BaseResult.build(BizCodeEnum.ERROR.getCode(),"用户名或密码错误",null);
        }
        return BaseResult.success();
    }

}
